<template>
  <div class="_wz_cent_main _wz_hiddenDangersTroubleshoot">
    <Card dis-hover :bordered="true" style="margin:0 0 10px;">
      <p slot="title">搜索</p>
      <div slot="extra">
        <Button type="info" size="small" v-if="!searchType" @click="searchType = true" icon="ios-arrow-down">更多</Button>
        <Button type="info" size="small" v-else @click="searchType = false" icon="ios-arrow-up">收起</Button>
      </div>
      <Form :model="formDataSearch" label-position="right" :label-width="100" inline>
        <FormItem label="排查周期">
          <i-select v-model.number="formDataSearch.riskCheckCycle" filterable placeholder="请选择排查周期" clearable>
            <Option :value="0">不限</Option>
            <Option :value="1">每天</Option>
            <Option :value="2">每周</Option>
            <Option :value="3">每旬</Option>
            <Option :value="4">每月</Option>
            <Option :value="5">每季</Option>
          </i-select>
        </FormItem>
        <FormItem label="排查类型">
          <i-select v-model="formDataSearch.riskPointTypeCode" filterable placeholder="请选择排查类型" clearable>
            <Option :value="item.code" :key="item.id" v-for="item in riskPointType">{{ item.value }}</Option>
          </i-select>
        </FormItem>
        <FormItem label="风险状态" v-if="searchType">
          <i-select v-model.number="formDataSearch.riskCheckState" filterable placeholder="请选择风险状态" clearable>
            <Option :value="0">无风险</Option>
            <Option :value="1">有风险</Option>
          </i-select>
        </FormItem>
        <FormItem label="整改状态" v-if="searchType">
          <i-select v-model.number="formDataSearch.riskHandleState" filterable placeholder="请选择整改状态" clearable>
            <Option :value="0">未整改</Option>
            <Option :value="1">已整改</Option>
          </i-select>
        </FormItem>
        <FormItem label="日期检索" v-if="searchType">
          <DatePicker type="daterange" v-model="dateRange" placeholder="请选择日期范围" style="width:96%"></DatePicker>
        </FormItem>

        <FormItem label="运输企业" v-if="searchType && !seeCarData">
          <i-select v-model="formDataSearch.deptUuid" filterable placeholder="请选择运输企业" clearable>
            <Option v-for="item in subDeptList" :value="item.uuid" :key="item.uuid">{{ item.deptName }}</Option>
          </i-select>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="searchSubmit">搜索</Button>
        </FormItem>
      </Form>
    </Card>
    <Card dis-hover :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra" v-if="!seeCarData">
        <Button type="primary" @click="addTableData">添加</Button>
        <Button type="success" @click="deriveData">导出</Button>
      </p>
      <wzTbale ref="wzTbale" :tableData="tableData"></wzTbale>
    </Card>
    <Drawer :title="formTitle" v-model="formOperation" width="670" :mask-closable="false" :draggable="true" :styles="formOperationStyles">
      <hiddenDangersTroubleshootAdd v-if="formOperation" @closeFun="closeFun" :selectData="riskListData"></hiddenDangersTroubleshootAdd>
    </Drawer>
    <Drawer :title="formTitle" v-model="hiddenTrouble" width="1100" :mask-closable="false" :draggable="true">
      <hiddenTroubleAdd v-if="hiddenTrouble" @closeFun="closeFun" :selectData="riskListData"></hiddenTroubleAdd>
    </Drawer>




    <Drawer title="详情" v-model="seeMoreType" width="990" :mask-closable="false" :draggable="true">
      <hiddenDangersTroubleshootDetails v-if="seeMoreType" :riskListData="riskListData"></hiddenDangersTroubleshootDetails>
    </Drawer>

  </div>
</template>
<script>


  import hiddenDangersTroubleshootAdd from "./hiddenDangersTroubleshootAdd";
  import hiddenDangersTroubleshootDetails from "./hiddenDangersTroubleshootDetails";
  import hiddenTroubleAdd from "./hiddenTroubleAdd";
  import wzTbale from "../../../components/table/table";

  export default {
    props: ['seeCarData'],//接收来自父组件的数据
    components: {
      hiddenDangersTroubleshootAdd,//添加
      hiddenDangersTroubleshootDetails,//查看详情
      hiddenTroubleAdd,//隐患添加
      wzTbale,//列表
    },
    data() {
      return {
        hiddenTrouble:false,//添加隐患
        riskPointType:[],//风险类型
        searchType:false,//搜索条件
        subDeptList: [],//运输企业
        dateRange: [],//时间范围
        riskListData: {},//
        seeMoreType: false,
        formDataSearch: {
          deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:''
        },//搜索表单数据
        formTitle: '',
        selectData: null,
        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },

        tableData: {
          url: this.apiUrl.webApi + "/safety/risk/riskCheckPageList",//请求地址
          method: 'post',//请求方式
          where: {deptUuid:!!this.seeCarData?this.seeCarData.deptUuid:''},//搜索条件
          height: 'auto',//表格高度 auto
          page: true,//分页是否使用
          columns: [//表头数据
            {type: 'index', width: 70, align: 'center', title: '序号'},
            {title: '登记编号', minWidth: 145, key: 'riskCheckNumber'},
            {
              title: '风险状态', minWidth: 140, render: (h, params) => {
                // 1：无隐患，2：有隐患，3：已处理，4：暂不处理，5：复查通过，6：复查未通过） riskState
                var color = '', test = '';
                if (params.row.riskCheckState == 1) {
                  color = 'error', test = '有风险';
                }else {
                  color = 'primary', test = '无风险';
                }
                return h('Tag', {
                  props: {
                    type: 'dot',
                    color: color
                  }
                }, test);
              }
            },
            {
              title: '整改状态', minWidth: 130, render: (h, params) => {
                // 1：无隐患，2：有隐患，3：已处理，4：暂不处理，5：复查通过，6：复查未通过） riskState
                var color = '', test = '';
                if (params.row.riskCheckState == 1) {
                  if (params.row.riskHandleState == 0) {
                    color = 'error', test = '未整改';
                  }else {
                    color = 'success', test = '已整改';
                  }
                }else{
                  color = 'primary', test = '无风险';
                }

                return h('Tag', {
                  props: {
                    type: 'dot',
                    color: color
                  }
                }, test);
              }
            },
            {
              title: '风险数量', minWidth: 90, key: 'riskCount',
              render: (h, params) => {

                return h('span', params.row.riskHandleCount + "/" + params.row.riskCheckCount+'项');
                // return h('span', {
                //   style: {"fontWeight": "900", "color": !!params.row.riskCheckCount ? '#ed4014' : '#19be6b'},
                // }, params.row.riskCheckCount);
              }
            },
            {title: '风险名称', minWidth: 200, key: 'riskPointName'},
            {title: '排查类型', minWidth: 100, key: 'riskPointTypeName'},
            {title: '排查周期', minWidth: 120, render: (h, params) => {
                // 0：不限，1：每天，2：每周，3：每旬，4：每月，5：每季
                var color = '', test = '';
                if (params.row.riskCheckCycle == 0) {
                  color = 'error', test = '不限';
                }else if (params.row.riskCheckCycle == 1){
                  color = 'primary', test = '每天';
                }else if (params.row.riskCheckCycle == 2){
                  color = 'primary', test = '每周';
                }else if (params.row.riskCheckCycle == 3){
                  color = 'primary', test = '每旬';
                }else if (params.row.riskCheckCycle == 4){
                  color = 'primary', test = '每月';
                }else if (params.row.riskCheckCycle == 5){
                  color = 'primary', test = '每季';
                }
                return h('span', test);
                // return h('Tag', {props: {type: 'dot',color: color}}, test);
              }},
            {title: '排查日期', minWidth: 90, key: 'riskCheckDate'},
            {title: '排查阶段', minWidth: 95, key: 'riskCheckPhase'},
            {title: '排查人员', minWidth: 95, key: 'riskCheckPerson'},
            {title: '运输企业', minWidth: 260, key: 'deptName'},
            {
              title: '操作', fixed: 'right', width:120,
              render: (h, params) => {
                var operationButtons = [];

                operationButtons.push(
                  h('span', {
                    style: {"color": "#2d8cf0", "cursor": "pointer"},
                    on: {
                      click: () => {
                        this.riskListData = params.row;
                        this.seeMoreType = true;
                      }
                    }
                  }, "查看")
                )

                var buttonList = [];


                //排查
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.formTitle = '排查';
                      this.riskListData = JSON.parse(JSON.stringify(params.row));
                      this.hiddenTrouble = true;
                    }
                  }
                }, '排查'))


                //修改
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.formTitle = '修改';
                      this.riskListData = JSON.parse(JSON.stringify(params.row));
                      this.formOperation = true;
                    }
                  }
                }, '修改'))
                //删除
                buttonList.push(h('DropdownItem', {
                  nativeOn: {
                    click: () => {
                      this.removeTableFun(params.row);
                    }
                  }
                }, '删除'))
                if (buttonList.length > 0 && !this.seeCarData) {
                  if(operationButtons.length > 0){
                    operationButtons.push(h('Divider', {props: {type: 'vertical',}}, ''))
                  }
                  operationButtons.push(h('Dropdown', {props: {trigger: "hover", transfer: true}},
                    [
                      h('span', {style: {"color": "#2d8cf0", "cursor": "pointer"}}, '更多▼'),
                      h('DropdownMenu', {slot: "list"}, buttonList)
                    ])
                  )
                }
                return operationButtons;
              }
            },
          ],
          format: {//返回数据格式
            list: ['data'],//数据list
            count: ['count'],//数据总量
          },
        },

      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      var that = this;

      //运输企业
      that.axios.post(that.apiUrl.coreDeptCoreDeptList,{}).then(res => {
        if (!!res) {
          that.subDeptList = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })


      //风险类型
      that.axios.post(that.apiUrl.systemDictSystemDictList, {type: "risk_point_type"}).then(res => {
        if (!!res) {
          that.riskPointType = res.data.data;
        }
      }).catch(err => {
        console.log("失败", err)
      })



    },
    methods: {//执行的方法
      removeTableFun(data) {//删除
        let that = this;
        that.$Modal.confirm({
          title: '删除提醒',
          content: '确定要删除选中的记录？',
          onOk: () => {
            that.spinShow = true;
            that.axios.post(that.apiUrl.webApi + "/safety/risk/riskCheckDelete", {uuid: data.uuid}).then(res => {
              that.spinShow = false;
              if (!!res) {
                that.$Message.success('数据删除成功.');
                that.$refs.wzTbale.tableLoadData();
              }
            }).catch(err => {
              console.log("失败", err)
            })
          },
          onCancel: () => {
            //点击取消
          }
        });
      },
      addTableData() {//table添加
        var that = this;
        that.formTitle = "添加";
        that.riskListData = {};
        that.formOperation = true;
      },
      searchSubmit() {//搜索
        this.formDataSearch.riskCheckStartDate = !!this.dateRange[0] ? this.public.timeFormatting(this.dateRange[0], "yyyy-MM-dd") : '';
        this.formDataSearch.riskCheckEndDate = !!this.dateRange[1] ? this.public.timeFormatting(this.dateRange[1], "yyyy-MM-dd") : '';
        this.tableData.where = JSON.parse(JSON.stringify(this.formDataSearch));
        this.$refs.wzTbale.tableLoadData(true);
      },
      //添加修改触发后关闭刷新
      closeFun(data) {
        var that = this;
        if (data) {//添加修改 刷新表单
          that.$refs.wzTbale.tableLoadData();
        }
        that.formOperation = false;
      },
      //导出
      deriveData() {
        var that = this;
        let formDataSearch = JSON.parse(JSON.stringify(this.formDataSearch));
        that.spinShow = true;
        that.axios.post(that.apiUrl.webApi + "/safety/risk/riskCheckExport", formDataSearch).then(res => {
          that.spinShow = false;
          if (!!res) {
            window.open(that.apiUrl.imgUrl + res.data.data)
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },

    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_hiddenDangersTroubleshoot {
    display: flex;
    flex-direction: column;


  }
</style>
